/*-------------------<-- Start of Description-->---------------------\ | Retrieve the observation number for a variable in a dataset by | | giving a value; | |---------------------<-- End of Description-->----------------------| |--------------------------------------------------------------------| |------------<-- Start of Files or Arguments Needed-->---------------| | arguments: | | indata - the name of a data set; | | varname - the 1st, 2nd or a valid variable name do you want; | | value - variable value you want to check; | | Basically, it retrieve the observation number for the variable in | | the datset for the given value; | |-------------<-- End of Files or Arguments Needed-->----------------| |--------------------------------------------------------------------| |------------------<-- Start of Files Created-->---------------------| | Example: %put %varobs(sashelp.library, 2, test); | | %put %varvalue(sashelp.library, 2, 3); | | Usage: %varobs(indata, varname, value); | \-------------------<-- End of Files Created-->---------------------*/ %macro varobs(indata, varname, value); /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 11-5-2002 11:27pm; | \--------------------------------------------*/ %local indata varname value; %let _varvaluedsid_=%sysfunc(open(&indata,i)); %if (&_varvaluedsid_) %then %do; %if (%chk_type(&varname)=1) %then %do; %if (&varname le %sysfunc(attrn(&_varvaluedsid_,NVARS))) %then %let varname=%sysfunc(varname(&_varvaluedsid_,&varname)); %else %do; %put ==> Alert! Dataset %data(&indata) does not have %trim(left(&varname)) variables!; . %goto finish; %end; %end; %else %do; %let _varvaluevarnum_=%sysfunc(varnum(&_varvaluedsid_,&varname)); %if (&_varvaluevarnum_ le 0) %then %do; %if (%quote(%upcase(%sysfunc(dequote(&varname)))) = %quote(NVARS)) %then %let varname=%sysfunc(varname(&_varvaluedsid_,%sysfunc(attrn(&_varvaluedsid_,NVARS)))); %else %do; %put ==> Alert! Dataset %data(&indata) does not have variable %trim(%quote(%left(%quote(%upcase(&varname)))))! A valid variable name; %put +++ or a valid option (NVARS, for the last variable for the dataset) is required.; . %goto finish; %end; %end; %end; %let value=%trim(%quote(%left(%quote(%upcase(%sysfunc(dequote(&value))))))); %syscall set(_varvaluedsid_); %let &varname=; %let _varvalueobsi_=0; %do %while((%quote(%upcase(&value)) ne %quote(%upcase(&&&varname))) and (%sysfunc(fetchobs(&_varvaluedsid_, %eval(&_varvalueobsi_+1))) eq 0)); %let _varvalueobsi_=%eval(&_varvalueobsi_+1); %end; %let rc=(%sysfunc(fetchobs(&_varvaluedsid_, &_varvalueobsi_)); %if (&_varvalueobsi_ lt %sysfunc(attrn(&_varvaluedsid_,NOBS))) or (%quote(%upcase(&value)) = %quote(%upcase(&&&varname))) %then %do; &_varvalueobsi_ %put --> Note: The smallest observation of variable %trim(%quote(%left(%quote(%upcase(&varname))))) with the value of %trim(%quote(%left(%quote(&value)))); %put +++ is %trim(%left(&_varvalueobsi_)).; %end; %else %if (&_varvalueobsi_ = %sysfunc(attrn(&_varvaluedsid_,NOBS))) and (%quote(%upcase(&value)) = %quote(%upcase(&&&varname))) %then %do; &_varvalueobsi_ %put --> Note: Only the last observation (%trim(%left(&_varvalueobsi_))) of variable %trim(%quote(%left(%quote(%upcase(&varname))))) has the value of %trim(%quote(%left(%quote(&value)))).; %end; %else %do; %put ==> Alert! Variable %trim(%quote(%left(%quote(%upcase(&varname))))) does not have a value of %quote(%upcase(&value)) in dataset %data(&indata)!; 0 %goto finish; %end; %let _varvaluerc_=%sysfunc(close(&_varvaluedsid_)); %end; %else %do; . %put ==> Alert! Dataset %data(&indata) does not exist!; %end; %finish: %mend varobs;